package com.litong.litongjiaoyu.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.litong.litongjiaoyu.annotation.IgnoreAuth;
import com.litong.litongjiaoyu.utils.HttpClientUtil;
import com.litong.litongjiaoyu.utils.JSONResult;
import com.litong.litongjiaoyu.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName AccessTokenController
 * @Description 获取AccessToken
 * @Author jingz
 * @Date 2020/1/4 23:11
 * @Version 1.0
 **/
@RestController
@RequestMapping("/content")
public class ContentController {

    /**
     * 用来请求微信的get和post
     */
    @Autowired
    private RedisUtil redisUtil;

    @IgnoreAuth
    @ResponseBody
    @GetMapping("/get")
    public JSONResult getContent() {
        String access_Token = (String) redisUtil.get("Token");

        if (access_Token != null) {
            //获取access_token
            String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + access_Token;


            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("type", "news");
            jsonObject.addProperty("offset", 0);
            jsonObject.addProperty("count", 20);
            String str = jsonObject.toString();

            JSONObject res = JSON.parseObject(HttpClientUtil.doPostJson(url, str));
            return JSONResult.ok(res);
        } else {
            return JSONResult.errorMsg("Token为空，暂时无法获取");
        }


    }
}
